/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.hanfu.domain.TableColumn;
import top.continew.admin.hanfu.domain.dto.TableColumnDto;
import top.continew.admin.hanfu.domain.vo.TableColumnVo;
import top.continew.admin.hanfu.service.impl.TableColumnServiceImpl;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-03-31
 */
@RestController
@RequestMapping("/table_column")
@RequiredArgsConstructor
public class TableColumnController {

    private final TableColumnServiceImpl tableColumnService;

    /**
     * 新增数据列
     * 
     * @param tableColumnDto
     * @return
     */
    @PostMapping("")
    public R<String> addColumn(@RequestBody TableColumnDto tableColumnDto) {

        TableColumn tableColumn = new TableColumn();
        tableColumn.setId(0);
        tableColumn.setTableId(tableColumnDto.getTableId());
        tableColumn.setTableColumnName(tableColumnDto.getTableColumnName());
        tableColumn.setTableColumnLabel(tableColumnDto.getTableColumnLabel());
        tableColumn.setTableColumnType(tableColumnDto.getTableColumnType());
        tableColumn.setInputType(tableColumnDto.getInputType());
        tableColumn.setPlaceholder(tableColumnDto.getPlaceholder());
        tableColumn.setOptionValue(tableColumnDto.getOptionValue());
        tableColumn.setUpdateBy(tableColumnDto.getUpdateBy());
        tableColumn.setUpdateTime(System.currentTimeMillis() + "");
        tableColumn.setStatus(1);

        tableColumnService.save(tableColumn);

        return R.success(tableColumn.getId() + "");
    }

    /**
     * 删除数据列
     * 
     * @param columnId
     * @return
     */
    @DeleteMapping("/{id}")
    public R delColumn(@PathVariable("id") Integer columnId) {

        boolean b = tableColumnService.removeById(columnId);

        return b ? R.success() : R.fail("删除失败");
    }

    /**
     * 获取数据列
     * 
     * @param id
     * @return
     */
    @GetMapping("/table/{id}")
    public R getTableColumnByTableId(@PathVariable("id") Integer id) {

        List<TableColumn> list = tableColumnService.lambdaQuery().eq(TableColumn::getTableId, id).list();

        if (list.isEmpty()) {
            return R.success(null);
        }

        List<TableColumnVo> tableColumnVos = BeanUtil.copyToList(list, TableColumnVo.class);

        return R.success(tableColumnVos);
    }

}
